﻿# -*- coding: utf-8 -*-

import os, re
import codecs

app_id_files = [
            ('app.yaml', r'application\:\s+(.*)'),
            ('webcontent/methods.py', r'http\://([^\.]*?).appspot.com/'),
            ('webcontent/templates/preview_en.html', r'http\://([^\.]*?).appspot.com/'),
            ('webcontent/templates/albums_index_en.html', r'http\://([^\.]*?).appspot.com/'),
            ('webcontent/templates/preview_cn.html', r'http\://([^\.]*?).appspot.com/'),
            ('webcontent/templates/albums_index_cn.html', r'http\://([^\.]*?).appspot.com/'),
            ('webcontent/media/sources.xml', r'http\://([^\.]*?).appspot.com/'),
        ]
        
google_analytics_id_files = [
            ('webcontent/templates/preview_en.html', r'_gat\._getTracker\("([^"]*?)"\);'),
            ('webcontent/templates/albums_index_en.html', r'_gat\._getTracker\("([^"]*?)"\);'),
            ('webcontent/templates/picasa_en.html', r'_gat\._getTracker\("([^"]*?)"\);'),
            ('webcontent/templates/preview_cn.html', r'_gat\._getTracker\("([^"]*?)"\);'),
            ('webcontent/templates/albums_index_cn.html', r'_gat\._getTracker\("([^"]*?)"\);'),
            ('webcontent/templates/picasa_cn.html', r'_gat\._getTracker\("([^"]*?)"\);'),
            ('webcontent/media/upgrade_notice/introduction.html', r'_gat\._getTracker\("([^"]*?)"\);'),

        ]

def changToAPPID(app_id):
    global app_id_files
    for (file, re_str) in app_id_files:
        print file, re_str
        f = codecs.open(file, 'r', "utf-8")
        file_content = f.read()
        f.close()
        
        #print file_content
        new_file_content = ""
        matches = re.finditer(re_str, file_content)
        last_match_end = 0;
        if matches:
            for match in matches:
                print match.start(1), match.end(1), match.group(1)
                if match.group(1)!=app_id:
                    new_file_content += file_content[last_match_end:match.start(1)]+app_id
                    last_match_end = match.end(1)
            new_file_content += file_content[last_match_end:]
            
            if new_file_content!=file_content:
                f = codecs.open(file, 'w', "utf-8")
                f.write(new_file_content)
                f.close()

def setAdmin(admin_emails_set=['appwill002@gmail.com']):
    file = 'webcontent/views.py'
    re_str = r'admin_emails\s+=\s+set\((.*)\)'
    print str(admin_emails_set)
    
    f = open(file, 'r')
    file_content = f.read()
    f.close()
    
    #print file_content
    match = re.search(re_str, file_content)
    if match:
        print match.start(1), match.end(1), match.group(1)
        file_content = file_content[:match.start(1)]+str(admin_emails_set)+file_content[match.end(1):]
        #print file_content
        f = open(file, 'w')
        f.write(file_content)
        f.close()

def setGoogleAnalyticsId(google_analytics_id):
    if google_analytics_id is None or google_analytics_id.strip()=="":
        return
    google_analytics_id = google_analytics_id.strip()
    
    global google_analytics_id_files
    for (file, re_str) in google_analytics_id_files:
        print file, re_str
        f = codecs.open(file, 'r', "utf-8")
        file_content = f.read()
        f.close()
        
        #print file_content
        new_file_content = ""
        matches = re.finditer(re_str, file_content)
        last_match_end = 0;
        if matches:
            for match in matches:
                print match.start(1), match.end(1), match.group(1)
                if match.group(1)!=google_analytics_id:
                    new_file_content += file_content[last_match_end:match.start(1)]+google_analytics_id
                    last_match_end = match.end(1)
            new_file_content += file_content[last_match_end:]
            
            if new_file_content!=file_content:
                f = codecs.open(file, 'w', "utf-8")
                f.write(new_file_content)
                f.close()
        
if __name__ == '__main__':
    changToAPPID('fashion-girls')
    #setAdmin(['appwill002@gmail.com'])
    